package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.wms.outer.in.OuterInAPI;
import com.example.tool.erp.sdk.api.wms.outer.in.dto.OuterInOrderCreateRequest;
import com.example.tool.erp.sdk.api.wms.outer.in.dto.OuterInOrderCreateResponse;
import com.example.tool.erp.sdk.api.wms.outer.in.dto.OuterInQueryWithDetailRequest;
import com.example.tool.erp.sdk.api.wms.outer.in.dto.OuterInQueryWithDetailResponse;
import com.example.tool.erp.sdk.impl.ApiFactory;

import java.util.ArrayList;
import java.util.List;

import static com.example.tool.erp.conf.HCParam.getClient;

public class OuterIn
{
//	public static void main(String[] args) throws WdtErpException
//	{
////		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
////				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
//		OuterInAPI api = ApiFactory.get(getClient(), OuterInAPI.class);
//
////		testCreateOrder(api);
//		testQueryWithDetail(api);
//	}

	private static void testCreateOrder(OuterInAPI api) throws WdtErpException
	{
		OuterInOrderCreateRequest.Order order = new OuterInOrderCreateRequest.Order();
		order.setWarehouseNo("xyxwms");

		List<OuterInOrderCreateRequest.OrderDetail> detailList = new ArrayList<>();
		OuterInOrderCreateRequest.OrderDetail detail = new OuterInOrderCreateRequest.OrderDetail();
		detailList.add(detail);

		detail.setSpecNo("gy01");
		detail.setNum(3);

		OuterInOrderCreateResponse response = api.createOrder(order, detailList, false);
		System.out.println(response.getStatus());
		System.out.println(response.getMessage());
	}

	private static void testQueryWithDetail(OuterInAPI api) throws WdtErpException
	{
		int pageSize = 10;
		Pager pager = new Pager(pageSize, 0, true);

		OuterInQueryWithDetailRequest request = new OuterInQueryWithDetailRequest();
		request.setStartTime("2023-05-01");
		request.setEndTime("2023-05-31");

		OuterInQueryWithDetailResponse response = api.queryWithDetail(request, pager);
		Integer total = response.getTotal();
		if (total == null || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false);
		for (int i = totalPage; i >= 0; --i)
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:"+pageSize+" page_no:"+i+" ");
			response = api.queryWithDetail(request, pager);
			System.out.println("处理数据");
		}
	}
}
